package com.qwiki.test;

import java.io.IOException;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.xml.sax.SAXException;

import com.qwiki.xml.WikiPageAsyncPrintHandler;
import com.qwiki.xml.WikiPageHandler;
import com.qwiki.xml.WikiPageMultiHandler;
import com.qwiki.xml.WikiXMLPageValidArticleFilter;
import com.qwiki.xml.WikiXmlEngine;

public class testEngine
{
  public static void main(String[] args) throws SAXException, IOException, InterruptedException
  {
    Logger.getRootLogger()
        .addAppender(new ConsoleAppender(new PatternLayout("%d{yyyy/MM/dd HH:mm:ss.SSS} %p [%c{1}] [%x] %m%n")));

    WikiXmlEngine engine = new WikiXmlEngine(args[0]);
    int numThreads = java.lang.Integer.parseInt(args[1]);

    WikiPageAsyncPrintHandler[] handlers = new WikiPageAsyncPrintHandler[numThreads];
    for (int i = 0; i < handlers.length; i++)
    {
      handlers[i] = new WikiPageAsyncPrintHandler("test", i);
      handlers[i].start();
    }
    WikiPageHandler handler = new WikiPageMultiHandler(handlers);
    handler.setFilter(new WikiXMLPageValidArticleFilter());
    engine.setHandler(handler);
    engine.process();
    for (int i = 0; i < handlers.length; i++)
    {
      handlers[i].drain();
      System.out.println(i + "%d drained");
      handlers[i].join();
    }
  }
}
